package com.wlb.es.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ESConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //此处为elasticsearch的用户名与密码，没有的话就不用此方法直接用下方注释的两行代码即可
        //记得修改ip地址
//        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("elasticsearch用户名","elasticsearch密码"));
//        return new RestHighLevelClient(RestClient.builder(
//                new HttpHost("192.168.255.200",9200,"http")).setHttpClientConfigCallback(hb -> hb.setDefaultCredentialsProvider(credentialsProvider)));
        return new RestHighLevelClient(RestClient.builder(//无需密码的连接方式，直接return
                new HttpHost("192.168.255.200",9200,"http")));
    }
}
